<?php
include_once 'titulo.php';
if ($_SESSION['IdUsuario']) {
    ?>

    <html>
        <head>
            <title>Vidracaria</title>
            <?php
            include_once 'MySQL.php';
            include_once 'cabecalho.php';
            include_once 'titulo.php';
            ?>
            <script type="text/javascript" src="script/jquery-1.6.3.min.js"></script>
            <script src="script/jquery.maskMoney.js" type="text/javascript"></script>
            <script type="text/javascript" src="script/jquery.maskedinput-1.3.min.js"></script>
            <script>
                $(function(){
                    $("#comissao").maskMoney({symbol:"R$",decimal:".",thousands:""});
                    $("#salario").maskMoney({symbol:"R$",decimal:".",thousands:""});
                });
            </script>
            <script>
                jQuery(function($){
                    $("#admissao").mask("99/99/9999");
                    $("#demissao").mask("99/99/9999");
                    $("#telefone").mask("(99) 9999-9999");
                    $("#celular").mask("(99) 9999-9999");
                    $("#cpf").mask("999.999.999-99");
                    $("#cep").mask("99.999.999");
                            
                });
            </script>
        </head>
        <body>
            <div class="total">
                <div class="cabecalho">
                </div>
                <?php
                include_once 'menuHorizontal.php';
                ?>
                <div class="conteudo">
                    <div class="subconteudo">
                        <?php
                        if (empty($_POST) || $_POST['ACAO'] == 'inserirFuncionario') {
                            inserirFuncionario();
                        }
                        ?>

                        <?php
                        if (isset($_POST['ACAO']) && $_POST['ACAO'] == 'pesquisarFuncionario') {//se não tiver nenhuma solicitaÃƒÂ§ão de POST então lista os usuÃƒÂ¡rios
                            listaFuncionario($_POST['PESQUISA']);
                        }
                        ?>

                        <?php
                        //Sá aparece a caixa de ediÃƒÂ§ão se houver um post com a aÃƒÂ§ão=updateCliente
                        if (isset($_POST['ACAO']) && $_POST['ACAO'] == 'updateFuncionario') {
                            updateFuncionario();
                        }
                        ?>

                    </div>
                </div>
                <div class="rodape">
                    <?php
                    include_once 'rodape.php';
                    ?>

                    <?php ?>


                </div>
            </div>
            <?php
        } else {
            header("location: session_expirada.php");
        }
        ?>
        <?php

        function inserirFuncionario() {
            ?>
            <form action="cadastro_funcionario.php" method="POST">
                <table class="tblPesquisar"><tr><td>
                            <label class="label3">Procurar Funcionário:</label>
                            <input type="text" name="PESQUISA" size="50px" class="InputMaior"/></td><td>
                            <input type="hidden" name="ACAO" value="pesquisarFuncionario" />
                            <input type="submit" value="" class="buttonPesquisar"/>
                </table>
            </form><br/><br/>

            <form action="script/acoes.php" method="POST" onsubmit="ValidaFormularioFunc">
                <input type="hidden" name="ACAO" value="inserirFuncionario">
                <table class="tblForm">
                    <thead>
                    <td colspan="2">
                        Cadastro de Funcionário</td>
                    </thead>
                    <tbody>
                        <tr><td colspan="2">
                    <center>Preencha os campos abaixo para cadastrar o Funcionário.
                        </br>Os campos com * são obrigatórios.</center><br/></td></tr>
                    <tr><td>
                            <label class="label">* Funcionário</label></td><td>
                            <input type = "text" name= "NOME" size="60px" required class="Input27px"/></td></tr>

                    <tr><td>
                            <label class="label">Telefone</label></td><td>
                            <input type = "text" name= "TELEFONE" id="telefone" size="15px" class="Input27px"/></td></tr>

                    <tr><td>
                            <label class="label">Celular</label></td><td>
                            <input type = "text" name= "CELULAR" id="celular" size="15px" class="Input27px"/></td></tr>

                    <tr><td>
                            <label class="label">CEP:</label></td><td>
                            <input type = "text" name= "CEP" id="cep" size="10px" class="Input27px"></td></tr>


                    <tr><td>
                            <label class="label">Endereco:</label></td><td>
                            <input type = "text" name= "ENDERECO" size="48px" class="Input27px"/>
                            Nº<input type ="text" name="NUMERO" size="6" class="Input27px"/>
                            Complemento<input type="text" name="COMPLEMENTO" size="12" class="Input27px"/>
                        </td></tr>
                    <tr><td>
                            <label class="label">* CPF</label></td><td>
                            <input type = "text" name= "CPF" size="15px" id="cpf" class="Input27px"/></td></tr>

                    <tr><td>
                            <label class="label">E-mail</label></td><td>
                            <input type = "text" name = "EMAIL" size="30px" class="Input27px"/></td></tr>

                    <tr><td>
                            <label class="label">* Sexo</label></td><td>
                            <input type = "radio" name = "SEXO" value="M"/>Masculino
                            <input type = "radio" name = "SEXO" value="F"/>Feminino</td></tr>

                    <tr><td>
                            <label class="label">*Cargo:</label></td><td>
                            <?php
                            echo "<select name='CARGO' required class='Input27px'>";
                            $sql = "SELECT *FROM tbl_cargo;";
                            $resultado = executaSQL($sql);
                            echo "<option value=''></option>";
                            while ($linha = mysqli_fetch_array($resultado)) {
                                echo "<option value='" . $linha['cag_id'] . "'>" . $linha['cag_descricao'] . "</option>";
                            }
                            echo "</select>";
                            ?>

                        </td></tr>

                    <tr><td>
                            <label class="label">* Salário</label></td><td>
                            <input type = "text" name = "SALARIO" size="10px" id="salario" class="Input27px"/></td></tr>
                    <tr><td>

                            <label class="label">Comissão</label></td><td>
                            <input type = "text" name = "COMISSAO" size="3px" id="comissao" maxlength="5" class="Input27px"/>

                            <label >%  - Data</label>
                            <input type = "text" name = "DATACOMISSAO" size="15px" readonly="true" class="inputBloqueado"/>
                        </td></tr>
                    <tr><td>

                            <label class="label">* Data Admissão</label></td><td>
                            <input type = "date" name = "ADMISSAO" id="admissao" class="Input27px"/></td></tr>
                    <tr><td>

                            <label class="label">Data Demissão</label></td><td>
                            <input type = "date" name = "DEMISSAO" id="demissao" class="Input27px"/></td></tr>

                    <tr><td>
                            <label class="label">* Ativo</label></td><td>
                            <input type = "radio" name = "ATIVO" value="1"/>Sim
                            <input type = "radio" name = "ATIVO" value="0"/>Não</td></tr>

                    </tbody>
                    <tfoot>
                        <tr> <td colspan="2">
                                <input type="submit" value="CADASTRAR" class="blackButtonYes"/>
                                <input type="reset" value="LIMPAR" class="blackButtonNo"/>
                            </td></tr>
                    </tfoot>

                </table>
            </form>
            <?php
        }

        function listaFuncionario($pesquisa) {
            ?>
            <form action="cadastro_funcionario.php" method="POST">
                <table class="tblPesquisar"><tr><td>
                            <label class="label3">Procurar Funcionário:</label>
                            <input type="text" name="PESQUISA" size="50px" class="InputMaior"/></td><td>
                            <input type="hidden" name="ACAO" value="pesquisarFuncionario" />
                            <input type="submit" value="" class="buttonPesquisar"/>
                </table>
            </form><br/><br/>
            <?php
            echo "<div style='width:98%; margin-top: 20px; margin-bottom: 20px; margin-left: auto; margin-right: auto;'>";
            global $conexao;

            $sql = "CALL sp_getFuncionarioByNome('$pesquisa')";
            $resultado = executaSQL($sql);

            //4-Consome o resultado. Se tiver
            echo "<table border='1' class='tblUsuarios'>"; //crio uma tabela para exibiÃƒÂ§ão de dados
            echo "<caption>Tabela de Funcionários.</caption>";

            echo "<thead>";
            echo "<th>" . "Sexo" . "</th>";
            echo "<th>" . "ID" . "</th>";
            echo "<th>" . "Nome" . "</th>";
            echo "<th>" . "Endereço" . "</th>";
            echo "<th>" . "Telefone" . "</th>";
            echo "<th>" . "Celular" . "</th>";
            echo "<th>" . "Cargo" . "</th>";
            echo "<th>" . "Salário" . "</th>";
            echo "<th>" . "Comissão" . "</th>";
            echo "<th>Status</th>";
            echo "<th colspan=2>" . "Ações" . "</th>";
            echo "</thead>";

            echo "<tbody>";

            while ($linha = mysqli_fetch_array($resultado)) {
                echo "<tr>";

                //testo o valor do campo sexo, e coloco o texto apropriado na tabela
                if ($linha["fun_sexo"] == "F") {
                    //echo "<td>" . "Feminino" . "</td>";
                    echo "<td>" . "<img src='style/imagens/female_user_icon256.png' style='display:block; margin-left:auto; margin-right:auto; width:30px; height:30px' />" . "</td>";
                } else {
                    //echo "<td>" . "Masculino" . "</td>";
                    echo "<td>" . "<img src='style/imagens/male_user_icon256.png' style='display:block; margin-left:auto; margin-right:auto; width:30px; height:30px' />" . "</td>";
                }

                echo "<td style='text-align:center;'>" . $linha["fun_id"] . "</td>";
                echo "<td>" . $linha["fun_nome"] . "</td>";
                echo "<td>" . $linha["fun_endereco"] . $linha["fun_numero"] . $linha["fun_complemento"] . "</td>";
                echo "<td>" . $linha["fun_telefone"] . "</td>";
                echo "<td>" . $linha["fun_celular"] . "</td>";
                echo "<td>" . $linha["cag_descricao"] . "</td>";
                echo "<td>" . $linha["fun_salario"] . "</td>";
                echo "<td>" . $linha["fun_comissao"] . "</td>";
                echo "<td>";
                if($linha['fun_ativo'] == 1){
                    echo "<label class='label'>Ativo<label>";
                }else{
                    echo "<label class='labelVermeDire'>Inativo</label>";
                }
                echo "<td>";

                echo "<form method='post' action='script/acoes.php' onsubmit='return confirm(\"Excluir Funcionario?\")'>"; //form de excluir
                echo "<input type='hidden' name='ACAO' value='deleteFuncionario'/>";
                echo "<input type='hidden' name='ID' value='" . $linha["fun_id"] . "'/>";
                echo "<input type='submit' value='' class='btnExcluir'/>"; //botão de excluir
                echo "</form>";

                echo "<td><form method='post' action='cadastro_funcionario.php'>"; //form de excluir
                echo "<input type='submit' value='' class='btnEditar' />"; //botão de editar
                echo "<input type='hidden' name='ACAO' value='updateFuncionario'/>";
                echo "<input type='hidden' name='ID' value='" . $linha["fun_id"] . "'/>";
                echo "<input type='hidden' name='NOME' value='" . $linha["fun_nome"] . "'/>";
                echo "<input type='hidden' name='TELEFONE' value='" . $linha["fun_telefone"] . "'/>";
                echo "<input type='hidden' name='CELULAR' value='" . $linha["fun_celular"] . "'/>";
                echo "<input type='hidden' name='CEP' value='" . $linha["fun_cep"] . "'/>";
                echo "<input type='hidden' name='ENDERECO' value='" . $linha["fun_endereco"] . "'/>";
                echo "<input type='hidden' name='NUMERO' value='" . $linha["fun_numero"] . "'/>";
                echo "<input type='hidden' name='COMPLEMENTO' value='" . $linha["fun_complemento"] . "'/>";
                echo "<input type='hidden' name='CPF' value='" . $linha["fun_cpf"] . "'/>";
                echo "<input type='hidden' name='EMAIL' value='" . $linha["fun_email"] . "'/>";
                echo "<input type='hidden' name='SEXO' value='" . $linha["fun_sexo"] . "'/>";
                echo "<input type='hidden' name='CARGO' value='" . $linha["fk_cag_id"] . "'/>";
                echo "<input type='hidden' name='SALARIO' value='" . $linha["fun_salario"] . "'/>";
                echo "<input type='hidden' name='COMISSAO' value='" . $linha["fun_comissao"] . "'/>";
                echo "<input type='hidden' name='DATACOMISSAO' value='" . $linha["fun_datacomissao"] . "'/>";
                echo "<input type='hidden' name='DATAADMISSAO' value='" . $linha["fun_dataadmissao"] . "'/>";
                echo "<input type='hidden' name='DATADEMISSAO' value='" . $linha["fun_datademissao"] . "'/>";
                echo "<input type='hidden' name='ATIVO' value='" . $linha["fun_ativo"] . "'/>";
                echo "</form>";

                echo "</td>";
                echo "</tr>";
            }
            echo "</tbody>";

            echo "<tfoot>";
            echo "</tfoot>";

            echo "</table>"; //finalizo tabela para exibiÃƒÂ§ão de dados
            //5 - encerro a conexao
        }
        ?>
        <?php

        function updateFuncionario() {
            ?>
            <form action="cadastro_funcionario.php" method="POST">
                <table class="tblPesquisar"><tr><td>
                            <label class="label3">Procurar Funcionário:</label>
                            <input type="text" name="PESQUISA" size="50px" class="InputMaior"/></td><td>
                            <input type="hidden" name="ACAO" value="pesquisarFuncionario" />
                            <input type="submit" value="" class="buttonPesquisar"/>
                </table>
            </form><br/><br/>

            <form action="script/acoes.php" method="POST" onsubmit="ValidaFormularioFunc">
                <input type="hidden" name="ACAO" value="updateFuncionario">
                <table class="tblForm">
                    <thead>
                    <td colspan="2">
                        Cadastro de Funcionário</td>
                    </thead>
                    <tbody>
                        <tr><td colspan="2">
                    <center>Preencha os campos abaixo para cadastrar o Funcionário.
                        </br>Os campos com * são obrigatários.</center><br/></td></tr>
                    <input type="hidden" name="ID"
                    <?php
                    if ($_POST['ID']) {
                        echo " value=\"";
                        echo $_POST['ID'];
                        echo "\" ";
                    }
                    ?>/>
                    <tr><td>
                            <label class="label">* Funcionário</label></td><td>
                            <input type = "text" name= "NOME" size="60px" class="Input27px" required
                            <?php
                            if ($_POST['NOME']) {
                                echo "value=\"";
                                echo $_POST['NOME'];
                                echo "\" ";
                            }
                            ?>/></td></tr>

                    <tr><td>
                            <label class="label">Telefone</label></td><td>
                            <input type = "text" name= "TELEFONE" maxlength="13" onkeyup="mascaraTel(this)" id="telefone" class="Input27px" size="15px"
                            <?php
                            if ($_POST['TELEFONE']) {
                                echo "value=\"";
                                echo $_POST['TELEFONE'];
                                echo "\" ";
                            }
                            ?>/></td></tr>

                    <tr><td>
                            <label class="label">Celular</label></td><td>
                            <input type = "text" name= "CELULAR" maxlength="13" onkeyup="mascaraTel(this)" id="celular" size="15px" class="Input27px"
                            <?php
                            if ($_POST['CELULAR']) {
                                echo "value=\"";
                                echo $_POST['CELULAR'];
                                echo "\" ";
                            }
                            ?>/></td></tr>

                    <tr><td>
                            <label class="label">CEP:</label></td><td>
                            <input type = "text" name= "CEP" maxlength="8" id="cep" onkeyup="mascaraCep(this)"size="10px" class="Input27px"
                            <?php
                            if ($_POST['CEP']) {
                                echo "value=\"";
                                echo $_POST['CEP'];
                                echo "\" ";
                            }
                            ?>/></td></tr>

                    <tr><td>
                            <label class="label">Endereco:</label></td><td>
                            <input type = "text" name= "ENDERECO" size="50px" class="Input27px"
                            <?php
                            if ($_POST['ENDERECO']) {
                                echo "value=\"";
                                echo $_POST['ENDERECO'];
                                echo "\" ";
                            }
                            ?>/>

                            Nº<input type ="text" name="NUMERO" size="6" class="Input27px"
                            <?php
                            if ($_POST['NUMERO']) {
                                echo "value=\"";
                                echo $_POST['NUMERO'];
                                echo "\" ";
                            }
                            ?>/>

                            Complemento<input type="text" name="COMPLEMENTO" size="15" class="Input27px"
                            <?php
                            if ($_POST['COMPLEMENTO']) {
                                echo "value=\"";
                                echo $_POST['COMPLEMENTO'];
                                echo "\" ";
                            }
                            ?>/>

                        </td></tr>
                    <tr><td>
                            <label class="label">* CPF</label></td><td>
                            <input type = "text" name= "CPF" id="cpf" size="15px" class="Input27px"
                            <?php
                            if ($_POST['CPF']) {
                                echo "value=\"";
                                echo $_POST['CPF'];
                                echo "\" ";
                            }
                            ?>/></td></tr>

                    <tr><td>
                            <label class="label">E-mail</label></td><td>
                            <input type = "text" name = "EMAIL" size="30px" class="Input27px"
                            <?php
                            if ($_POST['EMAIL']) {
                                echo "value=\"";
                                echo $_POST['EMAIL'];
                                echo "\" ";
                            }
                            ?>/></td></tr>

                    <tr><td>
                            <label class="label">* Sexo</label></td><td>
                            <?php
                            if (isset($_POST['SEXO']) && $_POST['SEXO'] == "F") {
                                echo "<input type =\"radio\" name = \"SEXO\" value=\"M\">Masculino</input>";
                                echo "<input type =\"radio\" name = \"SEXO\" value=\"F\" checked>Feminino</input></td></tr>";
                            } elseif ($_POST['SEXO'] == "M") {
                                echo "<input type =\"radio\" name = \"SEXO\" value=\"M\" checked>Masculino</input>";
                                echo "<input type =\"radio\" name = \"SEXO\" value=\"F\">Feminino</input></td></tr>";
                            } else {
                                echo "<input type =\"radio\" name = \"SEXO\" value=\"M\">Masculino</input>";
                                echo "<input type =\"radio\" name = \"SEXO\" value=\"F\">Feminino</input></td></tr>";
                            }
                            ?>

                    <tr><td>
                            <label class="label">*Cargo:</label></td><td>

                            <select name='CARGO' class="Input27px" required>
                                <option value=""></option>
                                <?php
                                $sql = "SELECT *FROM tbl_cargo;";
                                $resultado = executaSQL($sql);
                                while ($linha = mysqli_fetch_array($resultado)) {
                                    echo "<option value='" . $linha['cag_id'] . "'";
                                    if ($linha["cag_id"] == $_POST["CARGO"]) {
                                        echo "SELECTED";
                                    } else {
                                        echo "";
                                    };
                                    echo ">";
                                    echo $linha["cag_descricao"];
                                    echo "</option>";
                                }
                                echo "</select>";
                                ?>

                        </td></tr>

                    <tr><td>
                            <label class="label">* Salário</label></td><td>
                            <input type = "text" name = "SALARIO" id="salario" size="10px" class="Input27px"
                            <?php
                            if ($_POST['SALARIO']) {
                                echo "value=\"";
                                echo $_POST['SALARIO'];
                                echo "\" ";
                            }
                            ?>
                                   /></td></tr>
                    <tr><td>

                            <label class="label">Comissão</label></td><td>
                            <input type = "text" name = "COMISSAO" size="3px" id="comissao" maxlength="5" class="Input27px"
                            <?php
                            if ($_POST['COMISSAO']) {
                                echo "value=\"";
                                echo $_POST['COMISSAO'];
                                echo "\" ";
                            }
                            ?>/>

                            <label >%  - Data</label>
                            <input type = "text" name = "DATACOMISSAO" size="15px" readonly="true" class="inputBloqueado" class="Input27px"
                            <?php
                            if ($_POST['DATACOMISSAO']) {
                                echo "value=\"";
                                echo $_POST['DATACOMISSAO'];
                                echo "\" ";
                            }
                            ?>/>
                        </td></tr>
                    <tr><td>

                            <label class="label">* Data Admissão</label></td><td>
                            <input type = "date" name = "ADMISSAO" class="Input27px" id="admissao"
                            <?php
                            if ($_POST['DATAADMISSAO']) {
                                echo "value=\"";
                                echo $_POST['DATAADMISSAO'];
                                echo "\" ";
                            }
                            ?>/></td></tr>
                    <tr><td>

                            <label class="label">Data Demissão</label></td><td>
                            <input type = "date" name = "DEMISSAO" class="Input27px" id="demissao"
                            <?php
                            if ($_POST['DATADEMISSAO']) {
                                echo "value=\"";
                                echo $_POST['DATADEMISSAO'];
                                echo "\" ";
                            }
                            ?>/></td></tr>

                    <tr><td>
                            <label class="label">* Ativo</label></td><td>
                            <?php
                            if (isset($_POST['ATIVO']) && $_POST['ATIVO'] == "1") {
                                echo "<input type =\"radio\" name = \"ATIVO\" value=\"1\"checked>Sim</input>";
                                echo "<input type =\"radio\" name = \"ATIVO\" value=\"0\">Não</input></td></tr>";
                            } elseif ($_POST['ATIVO'] == "M") {
                                echo "<input type =\"radio\" name = \"ATIVO\" value=\"1\">Sim</input>";
                                echo "<input type =\"radio\" name = \"ATIVO\" value=\"0\"checked>Não</input></td></tr>";
                            } else {
                                echo "<input type =\"radio\" name = \"ATIVO\" value=\"1\">Sim</input>";
                                echo "<input type =\"radio\" name = \"ATIVO\" value=\"0\">Não</input></td></tr>";
                            }
                            ?>

                            </tbody>
                    <tfoot>
                        <tr> <td colspan="2">
                                <input type="submit" value="CADASTRAR" class="blackButtonYes"/>
                                <input type="reset" value="LIMPAR" class="blackButtonNo"/>
                            </td></tr>
                    </tfoot>

                </table>
            </form>


            <?php
        }
        ?>
        <?php

        function executaSQL($sql) {
            $mySQL = new MySQL;
            $resultado = $mySQL->execSP($sql);
            return $resultado;
        }
        ?>

    </body>
</html>
